<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <div id="app">x: {{x}} y: {{y}}</div>
        <script type="module">
            import {
                createApp, // 创建vue实例
                reactive, // 定义响应式数据（对象）
                onMounted, // 在setup中的钩子函数
                onUnmounted,
                toRefs, // 将响应式对象，转换为普通对象，只不过这个对象上的每个属性节点，都是ref()类型的响应式数据
            } from "./node_modules/vue/dist/vue.esm-browser.js";

            function useMousePosition() {
                const positions = reactive({
                    x: 0,
                    y: 0,
                });

                const update = e => {
                    positions.x = e.pageX;
                    positions.y = e.pageY;
                };

                onMounted(() => {
                    window.addEventListener("mousemove", update);
                });

                onUnmounted(() => {
                    window.removeEventListener("mousemove", update);
                });

                return toRefs(positions);
            }

            createApp({
                setup() {
                    // const positions = useMousePosition()
                    const { x, y } = useMousePosition();
                    return {
                        x,
                        y,
                    };
                },
                mounted() {
                    this.y = 100;
                },
            }).mount("#app");
        </script>
    </body>
</html>
